var hasFileToUpload = false;

_addCommonFileListener('#uploadFile01', function(file) {
	$('#file01').text(file.name);
	hasFileToUpload = true;
});

_addCommonFileListener('#uploadFile02', function(file) {
	$('#file02').text(file.name);
	hasFileToUpload = true;
});

_addCommonFileListener('#uploadFile03', function(file) {
	$('#file03').text(file.name);
	hasFileToUpload = true;
});

_addCommonFileListener('#uploadFile04', function(file) {
	$('#file04').text(file.name);
	hasFileToUpload = true;
});

_addCommonFileListener('#uploadFile05', function(file) {
	$('#file05').text(file.name);
	hasFileToUpload = true;
});

function _addImgFileListener(obj, fun) {
	$(obj).change(function() {
		try {
			var file = this.files[0];
			var reader = new FileReader();
			reader.onload = function() {
				var img = new Image();
				img.src = reader.result;
				img.onload = function() {
					var w = img.width,
						h = img.height;
					var canvas = document.createElement('canvas');
					var ctx = canvas.getContext('2d');
					$(canvas).attr({
						width: w,
						height: h
					});
					ctx.drawImage(img, 0, 0, w, h);
					var base64 = canvas.toDataURL('image/jpeg', 0.5);
					var imgData = {
						url: window.URL.createObjectURL(file),
						base64: base64,
						clearBase64: base64.substr(base64.indexOf(',') + 1),
						suffix: base64.substring(base64.indexOf('/') + 1, base64.indexOf(';')),
					};
					
					if(fun) {
						fun(imgData);
					}
				}
			}
			reader.readAsDataURL(this.files[0]);
		} catch(e) {
			alert('加载图片出现异常：' + e);
		}
	});
}

function _addCommonFileListener(obj, fun) {
	$(obj).change(function() {
		try {
			var file = this.files[0];
			if(fun) {
				fun(file);
			}
		} catch(e) {
			alert('加载文件出现异常：' + e);
		}
	});
}